/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mvc.controlador.sgcespol;


import java.sql.SQLException;
import mvc.modelo.basico.ModeloBasico;

import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mvc.helper.DebugBase;
import mvc.helper.FeedbackBase;
import mvc.modelo.basico.ModeloBasico;


/**
 *
 * @author PezEspada
 */
public class SGC1Controlador extends HttpServlet {
   HttpServletRequest request;
   HttpServletResponse response;
   HttpSession session;
   RequestDispatcher rd;
   ModeloBasico mybase = new ModeloBasico();
   ///> Feedback Retroalimentacion para el Cliente
   FeedbackBase feedback_mvc ;
   ///> Feedback Retroalimentacion para el Desarrollador
   DebugBase debug_mvc ;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param req servlet request
     * @param resp servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
        request = req;
        response = resp;
        session = request.getSession();
        feedback_mvc = (FeedbackBase)request.getAttribute("feedback_mvc");
        debug_mvc = (DebugBase)request.getAttribute("debug_mvc");
        
        modulo_init();



        //Capturas el param PRINCIPAL "sub" que puede ser un Atributo o un Parametro de entrada
        String submodulo_id = this.getValid_Request_Param_String( "sub" );
        request.setAttribute("submodulo_id", submodulo_id );
        /* El el Siguiente bloque lo primero que se debe hacer es:
         * 1.- Escoger el submodulo segun Request(parametro mid) o Redireccionamiento(atributo mid)
         * 2.- Ejecutar el Metodo que Realiza Todo la Logica del Negocio - Obtener info DB.
         * 3.- Configurar param en Request para Presentacion.
         * 4.- Redireccionar al Layout de Presentacion
         */

            if( submodulo_id.equalsIgnoreCase("proceso")){
                doProceso();
            }else if( submodulo_id.equalsIgnoreCase("indicador")  ){
                doIndicador();
            }else if( submodulo_id.equalsIgnoreCase("carrera")  ){
                doCarrera();
            }else{ //Aqui se presenta INDEX
                feedback_mvc.InsertarMensaje("redirect", "La pagina pedida no se a encontrado", feedback_mvc.M_WARNING );
                
                ///> feedback del Error
            }



        ///>Redireccionando al nivel del servidor
        ///>///////////////////////////////////////////////////////////////////
        ///>///////////////////////////////////////////////////////////////////
        cargaDebug();
        request.setAttribute("debug_mvc", debug_mvc );
        request.setAttribute("feedback_mvc", feedback_mvc );

         rd = request.getRequestDispatcher( (String)request.getAttribute("layoutActivo") );
         rd.forward(request, response);

       
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>






     private void doProceso(){
      debug_mvc.InsertarMensaje("Submodulo", "SGC1 - doProceso",0);
      //Capturas el param PRINCIPAL "acc" que puede ser un Atributo o un Parametro de entrada
      String accion_id = this.getValid_Request_Param_String( "acc" );
      request.setAttribute("accion_id", accion_id );
      request.setAttribute("contenido_id", accion_id );
      debug_mvc.InsertarMensaje("accion", accion_id, 0);

      if( accion_id.equalsIgnoreCase("agregar") ){
            debug_mvc.InsertarMensaje("Acc", "SGC1 - Agregar",0);
        //Que dispositivo estamos presentando COMPUTER | MOBILE
           if( this.esMobile_userAgent() ){ request.setAttribute("layoutActivo","WEB-INF/Layout/Mobile.jsp" );
           }else{  request.setAttribute("layoutActivo","WEB-INF/Layout/Computer.jsp" );     }
        //Component Information 1---------------------------------------------
        //Component Information 2---------------------------------------------
        //   ....
        //Component Information n---------------------------------------------
        //Set El Controlador de Contenido   < opcional >
           request.setAttribute("v_pre_contentFile_path","../JSPF/SGC1Content.jsp" );


      }else if( accion_id.equalsIgnoreCase("eliminar") ){
            debug_mvc.InsertarMensaje("Acc", "SGC1 - eliminar",0);
        //Que dispositivo estamos presentando COMPUTER | MOBILE
           if( this.esMobile_userAgent() ){ request.setAttribute("layoutActivo","WEB-INF/Layout/Mobile.jsp" );
           }else{  request.setAttribute("layoutActivo","WEB-INF/Layout/Computer.jsp" );     }
        //Component Information 1---------------------------------------------
        //Component Information 2---------------------------------------------
        //   ....
        //Component Information n---------------------------------------------
        //Set El Controlador de Contenido   < opcional >
           request.setAttribute("v_pre_contentFile_path","../JSPF/SGC1Content.jsp" );


      }else if( accion_id.equalsIgnoreCase("listar") ){
            debug_mvc.InsertarMensaje("Acc", "SGC1 - listar",0);
        //Que dispositivo estamos presentando COMPUTER | MOBILE
           if( this.esMobile_userAgent() ){ request.setAttribute("layoutActivo","WEB-INF/Layout/Mobile.jsp" );
           }else{  request.setAttribute("layoutActivo","WEB-INF/Layout/Computer.jsp" );     }
        //Component Information 1---------------------------------------------
        //Component Information 2---------------------------------------------
        //   ....
        //Component Information n---------------------------------------------
        //Set El Controlador de Contenido   < opcional >
           request.setAttribute("v_pre_contentFile_path","../JSPF/SGC1Content.jsp" );


      }else if( accion_id.equalsIgnoreCase("modificar") ){
            debug_mvc.InsertarMensaje("Acc", "SGC1 - modificar",0);
        //Que dispositivo estamos presentando COMPUTER | MOBILE
           if( this.esMobile_userAgent() ){ request.setAttribute("layoutActivo","WEB-INF/Layout/Mobile.jsp" );
           }else{  request.setAttribute("layoutActivo","WEB-INF/Layout/Computer.jsp" );     }
        //Component Information 1---------------------------------------------
        //Component Information 2---------------------------------------------
        //   ....
        //Component Information n---------------------------------------------
        //Set El Controlador de Contenido   < opcional >
           request.setAttribute("v_pre_contentFile_path","../JSPF/SGC1Content.jsp" );


      }else if( accion_id.equalsIgnoreCase("mostrar") ){
            debug_mvc.InsertarMensaje("Acc", "SGC1 - mostrar",0);
        //Que dispositivo estamos presentando COMPUTER | MOBILE
           if( this.esMobile_userAgent() ){ request.setAttribute("layoutActivo","WEB-INF/Layout/Mobile.jsp" );
           }else{  request.setAttribute("layoutActivo","WEB-INF/Layout/Computer.jsp" );     }
        //Component Information 1---------------------------------------------
        //Component Information 2---------------------------------------------
        //   ....
        //Component Information n---------------------------------------------
        //Set El Controlador de Contenido   < opcional >
           request.setAttribute("v_pre_contentFile_path","../JSPF/SGC1Content.jsp" );

        }else{
           debug_mvc.InsertarMensaje("Acc", "SGC1 - Error",0);
           //Que dispositivo estamos presentando COMPUTER | MOBILE
           if( this.esMobile_userAgent()){ request.setAttribute("layoutActivo","WEB-INF/Layout/ErrorMobile.jsp" );
           }else{  request.setAttribute("layoutActivo","WEB-INF/Layout/Error.jsp" );     }

           feedback_mvc.InsertarMensaje("redirect", "PROCESOS : error no enconrtado Accion", feedback_mvc.M_WARNING );
        }

    }///> FIn doProceso



     private void doIndicador(){
      debug_mvc.InsertarMensaje("Submodulo", "AY - doIndicador",0);
     }
     private void doCarrera(){
      debug_mvc.InsertarMensaje("Submodulo", "AY - doCarrera",0);
     }



     protected void modulo_init(){
        request.setAttribute("v_pre_contentFile_path","../JSPF/SGC1Content.jsp" );
        //GENERAL
        request.setAttribute("v_ge_modulo", "SYSTEM");       //Nombre del Modulo que estamos trabajando
        request.setAttribute("v_ge_acronimo","SYS" );            //ACRONIMO DEL MODULO
        //controlador deL content
        request.setAttribute("v_pre_content_switch","index" );

     }



     public Boolean esNumero(String palabra){
       Boolean valor = false;
       if (!palabra.trim().matches(".*\\D+.*")){
         valor = true;
       }
       return valor;

     }

      protected String getValid_Request_Param_String( String param_name ){
      String param_final ="index";
      if( request.getParameter(param_name) != null ){
        if( request.getParameter(param_name).trim() != null ){
          param_final = request.getParameter(param_name).trim();
        }
      }
    return param_final;
   }
    protected int    getValid_Request_Param_Int( String param_name ){
      String param_final ="1";
      if( request.getParameter(param_name) != null ){
        if( request.getParameter(param_name).trim() != null ){
          param_final = request.getParameter(param_name).trim();
        }
      }
    return Integer.parseInt(param_final);
   }


     protected String getValid_Request_Atribute_String( String attr_name ){
      String param_final ="index";
      if( request.getAttribute(attr_name) != null ){
          param_final = (String)request.getAttribute(attr_name);
      }
    return param_final;
 }

protected  Boolean esMobile_userAgent(){
  return false;
}

protected  void cargaDebug(){
  debug_mvc.InsertarMensaje("Modulo Trabajando:", "AYUDA",0);
  debug_mvc.InsertarMensaje("layout", (String)request.getAttribute("layoutActivo"),0);

}


}
